package com.ruoyi.recruit.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.recruit.constant.RecruitConstants;
import com.ruoyi.recruit.domain.RecruitContent;
import com.ruoyi.recruit.domain.RecruitInfo;
import com.ruoyi.recruit.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.recruit.domain.RecruitUser;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 应聘人员信息Controller
 * 
 * @author ruoyi
 * @date 2022-05-05
 */
@Controller
@RequestMapping("/recruit/user")
public class RecruitUserController extends BaseController
{
    private String prefix = "recruit/user";

    @Autowired
    private IRecruitUserService recruitUserService;

    @Autowired
    private IRecruitInfoService recruitInfoService;

    @Autowired
    private IRecruitContentService recruitContentService;

    @RequiresPermissions("recruit:user:view")
    @GetMapping(value = {"", "/{recruitId}"})
    public String user(@PathVariable(value = "recruitId", required = false) Long recruitId, ModelMap mmap)
    {
        RecruitInfo recruitInfo = new RecruitInfo();
        if(StringUtils.isNotNull(recruitId)){
            recruitInfo = recruitInfoService.selectRecruitInfoByRecruitId(recruitId);
        }
        mmap.put("recruitInfo", recruitInfo);
        mmap.put("infos", recruitInfoService.selectRecruitInfoList(new RecruitInfo()));
        return prefix + "/user";
    }

    /**
     * 查询应聘人员信息列表
     */
    @RequiresPermissions("recruit:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecruitUser recruitUser) {
        startPage();
        List<RecruitUser> list = recruitUserService.selectRecruitUserList(recruitUser);
        return getDataTable(list);
    }

    /**
     * 导出应聘人员信息列表
     */
    @RequiresPermissions("recruit:user:export")
    @Log(title = "应聘人员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecruitUser recruitUser) {
        List<RecruitUser> list = recruitUserService.selectRecruitUserList(recruitUser);
        ExcelUtil<RecruitUser> util = new ExcelUtil<RecruitUser>(RecruitUser.class);
        return util.exportExcel(list, "应聘人员信息数据");
    }

    /**
     * 导入应聘人员
     * @param file
     * @param recruitId
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "应聘人员", businessType = BusinessType.IMPORT)
    @RequiresPermissions("recruit:user:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, Long recruitId, boolean updateSupport) throws Exception
    {
        ExcelUtil<RecruitUser> util = new ExcelUtil<RecruitUser>(RecruitUser.class);
        List<RecruitUser> userList = util.importExcel(file.getInputStream());
        String message = recruitUserService.importUser(userList, recruitId, updateSupport, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("recruit:user:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<RecruitUser> util = new ExcelUtil<RecruitUser>(RecruitUser.class);
        return util.importTemplateExcel("应聘人员");
    }

    /**
     * 新增应聘人员信息
     */
    @GetMapping("/add/{recruitId}")
    public String add(@PathVariable("recruitId") Long recruitId, ModelMap mmap)
    {
        RecruitInfo recruitInfo = new RecruitInfo();
        if(recruitId != 0){
            recruitInfo = recruitInfoService.selectRecruitInfoByRecruitId(recruitId);
        }
        mmap.put("recruitInfo", recruitInfo);
        mmap.put("infos", recruitInfoService.selectRecruitInfoList(new RecruitInfo()));
        return prefix + "/add";
    }

    /**
     * 新增保存应聘人员信息
     */
    @RequiresPermissions("recruit:user:add")
    @Log(title = "应聘人员信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecruitUser recruitUser)
    {
        if (RecruitConstants.RECRUIT_USER_NOT_UNIQUE.equals(recruitUserService.checkIdCardUnique(recruitUser)))
        {
            return error("新增报名人员失败，报名人员已存在");
        }
        recruitUser.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(recruitUserService.insertRecruitUser(recruitUser));
    }

    /**
     * 修改应聘人员信息
     */
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        RecruitUser recruitUser = recruitUserService.selectRecruitUserByUserId(userId);
        mmap.put("recruitUser", recruitUser);
        mmap.put("infos", recruitInfoService.selectRecruitInfoList(new RecruitInfo()));
        return prefix + "/edit";
    }

    /**
     * 修改保存应聘人员信息
     */
    @RequiresPermissions("recruit:user:edit")
    @Log(title = "应聘人员信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecruitUser recruitUser)
    {
        if (RecruitConstants.RECRUIT_USER_NOT_UNIQUE.equals(recruitUserService.checkIdCardUnique(recruitUser)))
        {
            return error("修改报名人员失败，报名人员已存在");
        }
        recruitUser.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(recruitUserService.updateRecruitUser(recruitUser));
    }

    /**
     * 删除应聘人员信息
     */
    @RequiresPermissions("recruit:user:remove")
    @Log(title = "应聘人员信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recruitUserService.deleteRecruitUserByUserIds(ids));
    }

    /**
     * 校验身份证号是否唯一
     */
    @PostMapping("/checkIdCardUnique")
    @ResponseBody
    public String checkIdCardUnique(RecruitUser recruitUser)
    {
        return recruitUserService.checkIdCardUnique(recruitUser);
    }

    /**
     * 查询报名人员列表
     */
    @PostMapping("/userList")
    @ResponseBody
    public AjaxResult userList(Long contentId)
    {
        List<RecruitUser> userList = recruitUserService.selectRecruitUserByContentId(contentId);
        return success(userList);
    }
}
